<template>
  <div class="page-header-index-wide">
    <a-card :loading="loading" :bordered="false" :body-style="{padding: '0'}">
      <div class="salesCard">
        <div class="extra-wrapper" slot="tabBarExtraContent">
          <div class="extra-item">
            <!--<a>今日</a>
            <a>本周</a>
            <a>本月</a>-->
            <j-multi-select-tag :style="{width: '256px'}" v-model="storeId" placeholder="店铺" dictCode="stm_store_info,name,store_id,status!=0"/>
          </div>

          <a-range-picker :style="{width: '256px'}" @change="onChange" :defaultValue="[moment(new Date(Date.now() -7*24*3600*1000 ), dateFormat), moment(new Date(), dateFormat)]" :format="dateFormat"/>

          <a-button style="margin-left: 18px" type="primary" @click="handlerQuery" icon="search">查询</a-button>
        </div>
        <a-tabs v-model="activeKey" :default-active-key="activeKey" @change="handlerTabsChange" size="large" :tab-bar-style="{marginBottom: '0px', paddingLeft: '8px'}">
          <a-tab-pane loading="true" v-for="(config,configIndex) in showConfigList" :tab="config.name" :key="config.code">
            <a-row>
              <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
                <j-report-table :code="config.code" :startTime="start" :endTime="end" :storeId="storeId" :version="config.version"></j-report-table>
              </a-col>
            </a-row>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-card>


  </div>
</template>

<script>
  import moment from 'moment';
  import { httpAction } from '@/api/manage'
  import { getAction } from '@/api/manage'
  import JReportTable from '@/components/jeecgbiz/JReportTable'
  import JMultiSelectTag from '@/components/dict/JMultiSelectTag'

  export default {
    name: "SalesReport",
    components: {
      JReportTable,
      JMultiSelectTag
    },
    data() {
      return {
        showConfigList:[],
        start: this.getDay(-7),
        end: this.getDay(0),
        storeId: '',
        //value: '',
        activeKey: '',
        time: 0,
        dateFormat: 'YYYY-MM-DD',
        loading: true,
        url: {
          queryByCode: "/report/salesReport/queryByCode",
          queryDataSourceByCode: "/report/salesReport/queryDataSourceByCode",
          queryShowConfig: "/report/salesReport/queryShowConfig",
        }
      }
    },
    created() {
      console.log(this.$route.path);
      setTimeout(() => {
        this.loading = !this.loading
      }, 1000)
      this.initReportInfo();
    },
    watch:{
      '$route': function (to, from) {
        if (to.path.startsWith('/report/')){
          this.initReportInfo();
        }
      }
    },
    methods: {
      moment,
      handlerTabsChange() {
        this.handlerQuery();
      },
      handlerQuery() {
        let index = this.showConfigList.findIndex(c=>c.code==this.activeKey);
        if (index>=0){
          this.showConfigList[index].version++;
          this.$set(this.showConfigList, index, this.showConfigList[index]);
        }
        console.log(this.showConfigList.filter(c=>c.code==this.activeKey)[0].version)
      },
      onChange(date, dateString) {
        this.start = dateString[0];
        this.end = dateString[1];
        console.log("onChange start",this.start);
        this.handlerQuery();
      },
      getDay(day) {
        var nowDate = new Date();
        var date = new Date();
        date.setDate(nowDate.getDate() + day);
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
          strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
      },
      initReportInfo () {
        getAction(this.url.queryShowConfig,{url:this.$route.path}).then((res) => {
          if (res.success) {
            this.showConfigList = res.result;
            this.showConfigList.forEach(c=>{
              c.version = 0;
            });
            if(this.showConfigList.length >0) {
              console.log("this.activeKey",this.activeKey);
              this.activeKey = this.showConfigList[0].code;
              // this.$nextTick(()=>{
              //   this.handlerQuery()
              // });
            }
          } else {
            this.$message.warning(res.message)
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .circle-cust{
    position: relative;
    top: 28px;
    left: -100%;
  }
  .extra-wrapper {
    line-height: 55px;
    padding-right: 24px;
    text-align: right;

    .extra-item {
      display: inline-block;
      margin-right: 24px;
      text-align: left;

      a {
        margin-left: 24px;
      }
    }
  }

  /* 首页访问量统计 */
  .head-info {
    position: relative;
    text-align: left;
    padding: 0 32px 0 0;
    min-width: 125px;

    &.center {
      text-align: center;
      padding: 0 32px;
    }

    span {
      color: rgba(0, 0, 0, .45);
      display: inline-block;
      font-size: .95rem;
      line-height: 42px;
      margin-bottom: 4px;
    }
    p {
      line-height: 42px;
      margin: 0;
      a {
        font-weight: 600;
        font-size: 1rem;
      }
    }
  }
</style>